package com.base.thread;

import java.util.ArrayList;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import com.base.thread.handler.Handler;
import com.base.thread.handler.SysHandler;
import com.base.thread.msg.Packet;

/**
 * �̳߳�����
 * @author Administrator
 *
 */
public class ThreadPool {
	
	//���߳���Ŀ
	private Integer subListSize = Runtime.getRuntime().availableProcessors();
	
	private MonitorThread monitorThread = new MonitorThread();
	
	private List<SubThread> subThreadList = new ArrayList<SubThread>(subListSize);
	

	public ThreadPool(String monitorThreadName,String subThreadName,Handler handler) {
		
		
		for (int index = 0 ; index < subListSize ; index++ ) {
			SubThread subThread = new SubThread(); 
			subThreadList.add(subThread);
			subThread.init(subThreadName+index, new  LinkedBlockingQueue<Packet>(),handler);
			subThread.start();
		}
		
		monitorThread.init(monitorThreadName, subThreadList);
		monitorThread.start();
		
	}
	
	
	public boolean addMsg(Packet packet) {
		
		boolean rtv = false;
		
		if(null != packet && packet.getId() >= 0){
			
			Integer subThreadNum = packet.getId() % subThreadList.size();
//			System.out.println("packet.getId()="+ packet.getId() +"subThreadNum="+subThreadNum);
			SubThread subThread = subThreadList.get(subThreadNum);
			rtv = subThread.addMsg(packet);
		}
		
		return rtv;
	}
	
	
	
	
	
	
	
	
	
	
	
	
}
